﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace LungSoundWizard.ViewModel
{
    public class WizardContainerViewModel : ObservableBase
    {
        WizardViewModelBase _currentPage;
        public WizardViewModelBase CurrentPage
        {
            get { return _currentPage; }
            set { SetProperty(ref _currentPage, value); }
        }

        readonly Stack<WizardViewModelBase> _prevPages = new Stack<WizardViewModelBase>();
        public Stack<WizardViewModelBase> PrevPages
        {
            get { return _prevPages; }
        }
        

        RelayCommand _nextPageCommand;
        public ICommand NextPageCommand
        {
            get
            {
                if (_nextPageCommand == null)
                {
                    _nextPageCommand = new RelayCommand(p => this.NextPage(),
                        p => CurrentPage.CanGoNext);
                }
                return _nextPageCommand;
            }
        }


        RelayCommand _prevPageCommand;
        public ICommand PrevPageCommand
        {
            get
            {
                if (_prevPageCommand == null)
                {
                    _prevPageCommand = new RelayCommand(p => this.PrevPage(),
                        p => PrevPages.Count > 0);
                }
                return _prevPageCommand;
            }
        }

        public WizardContainerViewModel()
        {
            CurrentPage = new SelectTaskViewModel();
        }

        void NextPage()
        {
            PrevPages.Push(CurrentPage);

            CurrentPage.OnHide();

            CurrentPage.RequestNextPage -= CurrentPage_RequestNextPage;

            CurrentPage = CurrentPage.NextPage;

            CurrentPage.RequestNextPage += CurrentPage_RequestNextPage;

            CurrentPage.OnShow();
        }

        void CurrentPage_RequestNextPage(object sender, EventArgs e)
        {
            NextPage();
        }

        void PrevPage()
        {
            CurrentPage.OnHide();

            CurrentPage.RequestNextPage -= CurrentPage_RequestNextPage;

            CurrentPage = PrevPages.Pop();

            CurrentPage.RequestNextPage += CurrentPage_RequestNextPage;

            CurrentPage.OnShow();
        }
    }
}
